package com.xxx.yuedong.oss.controller;

import com.xxx.yuedong.common.R;
import com.xxx.yuedong.oss.controller.form.DeleteCosFileForm;
import com.xxx.yuedong.oss.utils.CosUtil;
import com.xxx.yuedong.oss.utils.TypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.HashMap;

@RequestMapping("/cos")
@RestController
@Api(tags = "云存储相关接口")
public class CosController {
    @Autowired
    private CosUtil cosUtil;

    @PostMapping("/uploadCosFile")
    @ApiOperation(value = "文件上传")
    public R uploadCosFile(@RequestParam("files") MultipartFile[] files, @RequestParam("type") String type) {
        TypeEnum typeEnum = TypeEnum.findByKey(type);
        try {
            HashMap map = cosUtil.uploadFile(files, typeEnum);
            return R.ok(map);
        } catch (IOException e) {
            throw new RuntimeException();
        }
    }

    @PostMapping("/deleteCosFile")
    @ApiOperation(value = "删除文件")
    public R deleteCosFile(@Valid @RequestBody DeleteCosFileForm form) {
        cosUtil.deleteFile(form.getPathes());
        return R.ok();
    }
}
